﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EasyLift.TMS.Web.Models;
using EasyLift.TMS.Web.Helpers;

namespace CoreBiz.Web.Controllers
{
    public class DriverController : Controller
    {

        DriverRepository repository = new DriverRepository();
        //
        // GET: /Driver/

        //public ActionResult Index(int? page)
        //{
        //    int pageSize = 10;
        //    var Drivers = repository.GetAllData();
        //    var paginatedDrivers = new PaginatedList<Driver>(Drivers, page ?? 0, pageSize);
        //    return View("Index", paginatedDrivers);
        //}

        public ActionResult Index()
        {
            return View("Index");
        }

        public ActionResult Status()
        {
            return View("Status");
        }

        public JsonResult GridData(string sidx, string sord, int page, int rows, bool _search, string searchField, string searchOper, string searchString)
        {

            var allData = repository.GetAllData(sidx, sord, _search, searchField, searchOper, searchString);

            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = allData.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            var paginatedAllData = new PaginatedList<Driver>(allData, pageIndex, pageSize);


            var JsonData = new
            {
                total = totalPages,
                page = page,
                records = totalRecords,
                rows = (
                    from p in paginatedAllData
                    select new
                    {
                        id = p.CivilID,
                        cell = new string[] {
                            p.CivilID.ToString(),
                            p.FirstName,
                            p.LastName,
                            p.ArabicFirstName,
                            p.CountryCode,
                            p.PassportNo,
                            p.DriverMissionStatus.DriverMissionStatusDescription
                        }
                    }
                ).ToArray()
            };

            return Json(JsonData, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GridDataByStatus(string sidx, string sord, int page, int rows, bool _search, string searchField, string searchOper, string searchString, string status)
        {

            var allData = repository.GetAllDataByStatus(sidx, sord, _search, searchField, searchOper, searchString,status);

            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = allData.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            var paginatedAllData = new PaginatedList<Driver>(allData, pageIndex, pageSize);


            var JsonData = new
            {
                total = totalPages,
                page = page,
                records = totalRecords,
                rows = (
                    from p in paginatedAllData
                    select new
                    {
                        id = p.CivilID,
                        cell = new string[] {
                            p.CivilID.ToString(),
                            p.FirstName,
                            p.LastName,
                            p.ArabicFirstName,
                            p.CountryCode,
                            p.PassportNo,
                            p.DriverMissionStatus.DriverMissionStatusDescription
                        }
                    }
                ).ToArray()
            };

            return Json(JsonData, JsonRequestBehavior.AllowGet);
        }



        //
        // GET: /Driver/Details/5

        public ActionResult Details(string id)
        {
            Driver Driver = repository.GetByID(id);
            if (Driver == null)
                return View("NotFound");
            else
                return View("Details", Driver);
        }

        //
        // GET: /Driver/Create

        public ActionResult Create()
        {
            Driver pf = new Driver();
            pf.FirstName = "New Driver";
            pf.DriverMissionStatusCode = "1";
            pf.DriverHRStatusCode = "1";
            return View(new DriverViewModel(pf));
        }

        //
        // POST: /Driver/Create

        [HttpPost]
        public ActionResult Create(Driver Driver)
        {

            //UpdateModel(Driver);
            try
            {
                if (String.IsNullOrEmpty(Driver.CivilID))
                {
                    ModelState.AddModelError("CivilID", "CivilID is required");
                }

                if (ModelState.IsValid)
                {

                    repository.Add(Driver);
                    repository.Save();
                }
                else
                {
                    return View(new DriverViewModel(Driver));
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View(new DriverViewModel(Driver));
            }

        }

        //
        // GET: /Driver/Edit/5

        public ActionResult Edit(string id)
        {
            try
            {
                Driver Driver = repository.GetByID(id);
                if (Driver == null)
                    return View("NotFound");

                return View(new DriverViewModel(Driver));
            }
            catch
            {
                return View();
            }
        }

        //
        // POST: /Driver/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, Driver Driver)
        {
            try
            {
                //UpdateModel(Driver);
                repository.Update(Driver);
                repository.Save();

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("DataVersionConflict", ex);

                return View(new DriverViewModel(Driver));

            }

        }

        //
        // GET: /Driver/Delete/5

        public ActionResult Delete(string id)
        {
            Driver Driver = repository.GetByID(id);

            return View(Driver);
        }

        //
        // POST: /Driver/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            try
            {
                Driver Driver = repository.GetByID(id);

                repository.Delete(Driver);
                repository.Save();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }

        }

    }
}
